#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>

void sigHandler(int sig){
    printf("Recive signal %d %s\n", sig, strsignal(sig));
}

int main(int argc, char **argv){
    struct sigaction act;
    sigset_t newSet, oldSet;

    printf("Program %s, pid %d\n", argv[0], getpid());
    act.sa_handler = &sigHandler;
    for(int i = 1; i < NSIG; i++){
        if(sigaction(i, &act, NULL) == -1)
            printf("Error sigaction signal %d %s \n", i, strsignal(i));        
    }

    puts("Set handler for all signales");

    if(sigfillset(&newSet) == -1){
        printf("Error sigfillset %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    if(sigprocmask(SIG_SETMASK, &newSet, &oldSet) == -1){
        printf("Error block signals sigprocmask %s\n", strerror(errno));
        exit(EXIT_FAILURE);        
    }
    puts("Blocks all signal, and sleep");

    sleep(17);
    puts("Unblock signals");
    if(sigprocmask(SIG_SETMASK, &oldSet, NULL) == -1){
        printf("Error revert signals sigprocmask %s\n", strerror(errno));
        exit(EXIT_FAILURE);        
    }

    exit(EXIT_SUCCESS);

}